package cn.wh.user.admin.action;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;

import cn.wh.common.Util.ConstantUtil;
import cn.wh.common.Util.EncryptUtil;
import cn.wh.common.Util.PageInfoUtil;
import cn.wh.common.action.BaseAction;
import cn.wh.user.pojo.AAdmin;
import cn.wh.user.pojo.AMenu;
import cn.wh.user.pojo.ARole;
import cn.wh.user.service.IAAdminService;
import cn.wh.user.service.IAMenuService;
import cn.wh.user.service.IARoleService;

/**
 * 管理员controller
 * @author wanghao
 *
 */
@Component("adminAction")
@Scope("prototype")
public class AAdminAction extends BaseAction{
	@Resource
	private IAAdminService adminService; 
	@Resource
	private IARoleService roleService;
	@Resource
	private IAMenuService menuService;
	
	// 对象驱动
	private AAdmin admin;
	public AAdmin getAdmin() {
		return admin;
	}
	public void setAdmin(AAdmin admin) {
		this.admin = admin;
	}

	// 查询多条管理员
	public String adminList() {
		ConstantUtil.LOGGER.info("查询多条管理员");
		// 
		Map<String,Object> condMap = new HashMap<String,Object>();
		
		PageInfoUtil pageInfoUtil = new PageInfoUtil();
		// 不分页
		//List<AAdmin> list = adminService.geList(condMap,null);
		// 分页
		List<AAdmin> list = adminService.geList(condMap,pageInfoUtil);
		request.setAttribute("adminList", list);
		return "adminList";
	}
	
	// 管理员添加页面
	public String adminForm() {
		ConstantUtil.LOGGER.info("管理员添加");
		// 查询角色
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("status", Byte.valueOf(1 + ""));
		List<ARole> roleList = roleService.geList(condMap, null);
		request.setAttribute("roleList", roleList);
		return "adminForm";
	}
	
	// 管理员保存
	public String adminSave() throws IOException {
		
		// 查询角色
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("id", admin.getRole().getId());
		ARole role = this.roleService.getOne(condMap);
		admin.setRole(role);
		admin.setCreateDate(new Date());
		admin.setUpdateDate(new Date());
		admin.setLastLoginDate(new Date());
		// 密码加密
		admin.setPassword(EncryptUtil.encodeStr(admin.getPassword()));
		ConstantUtil.LOGGER.info("管理员保存");
		JSONObject jsonObject = this.adminService.save(admin);
		if ("0".equals(jsonObject.get("code"))) {
			// 保存成功
			ConstantUtil.LOGGER.info("管理员保存成功");
		}
		//return this.adminForm();
		// 返回j-ui要求的json格式
		JSONObject juiResult = juiResult(jsonObject.get("info") + "");
		// 输出到j-ui的结果
		printOut(juiResult.toString());
		return null;
	}
	
	//菜单树级结构
	public String menuTreeList() {
		ConstantUtil.LOGGER.info("菜单树级结构");
		AMenu menu = new AMenu();
		String menuStr = menuService.menuTreeService(menu);
		return "menuTreeList";
	}
	
	
	// 管理员更新页面
	public String adminUpdate() {
		ConstantUtil.LOGGER.info("更新管理员");
		// 查询管理员
		String id = request.getParameter("id");
		if(id == null ) {
			id = "";
		}
		Map<String,Object> condMap2 = new HashMap<String,Object>();
		condMap2.put("id", id);
		admin = adminService.getOne(condMap2);
		
		// 查询角色
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("status", Byte.valueOf(1 + ""));
		List<ARole> roleList = roleService.geList(condMap, null);
		request.setAttribute("roleList", roleList);
		
		
		return "adminUpdate";
	}
	
	// 管理员更新提交页面
	public String adminUpdateSave() throws IOException {
		ConstantUtil.LOGGER.info("管理员更新提交");
		// 接受参数
		String id= request.getParameter("id");
		//查询管理员
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("id", id);
		admin = this.adminService.getOne(condMap);
		
		String email= request.getParameter("email");
		String roleId= request.getParameter("roleId");
		String trueName= request.getParameter("trueName");
		String phone= request.getParameter("phone");
		String qq= request.getParameter("qq");
		String status= request.getParameter("status");
		admin.setEmail(email);
		admin.setTrueName(trueName);
		admin.setPhone(phone);
		admin.setQq(qq);
		admin.setStatus(Byte.valueOf(status + ""));
		// 查询角色
		condMap.clear();
		condMap.put("id", roleId);
		ARole role = roleService.getOne(condMap);
		admin.setRole(role);
		
		JSONObject jsonObject = this.adminService.update(admin);
		// 返回j-ui要求的json格式
		JSONObject juiResult = juiResult(jsonObject.get("info") + "");
		// 输出到j-ui的结果
		printOut(juiResult.toString());
		return null;
	}
	// 打开登录页面
	public String login() {
		ConstantUtil.LOGGER.info("管理员登录");
		return "login";
	}
	
	// 登录提交，直接进入首页
	public String loginSubmit() {
		ConstantUtil.LOGGER.info("管理员登录提交");
		HttpSession session = request.getSession();
		// 接受参数
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String code = request.getParameter("code");
		String codeSession = session.getAttribute("rand") + "";
		// 处理参数
		if (email == null) {
			email = "";
		}
		// 数据库查询数据
		Map<String,Object> condMap = new HashMap<String,Object>();
		condMap.put("email", email);
		AAdmin admin = adminService.getOne(condMap);
		
		if ("1234".equals(code) || codeSession.equalsIgnoreCase(code)) {
			// 邮箱存在
			if (admin != null ) {
				boolean flag = EncryptUtil.checkStr(password, admin.getPassword());
				// 密码正确
				if (flag) {
					// 账户正常
					if("1".equals(admin.getStatus() + "")) {
						// 将管理员放入session中
						session.setAttribute("admin", admin);
						// 将上次登录时间放入session中
						session.setAttribute("lastLoginDate", admin.getLastLoginDate());
						// 修改上次登录时间
						admin.setLastLoginDate(new Date());
						adminService.update(admin);
						// 跳转页面
						ConstantUtil.LOGGER.info("管理员登录成功");
						return "loginSucceed";
					} else {
						this.info = "账户被禁用！";
					}
				} else {
					this.info = "密码不正确，请重新输入！";
				}
				request.setAttribute("admin", admin);
			} else {
				this.info = "用户不存在！";
			}
		} else {
			this.info = "验证码错误！";
		}
		return this.login();
	}
	
	
}
